const ps = {
  Program(node, env) {
    const result = {
      type: 'Program',
      links: [{
        name: 'body',
        list: [],
      }],
    };
    let item;
    for (item of node.body) {
      result.links[0].list.push(ps.process(item, env));
    }
    env.root = result;
    return result;
  },
  VariableDeclaration(node, env) {
    const result = {
      type: 'VariableDeclaration',
      value: node.kind,
      links: [{
        name: 'declarations',
        list: [],
      }],
    };
    let item;
    const kind = node.kind;
    for (item of node.declarations) {
      result.links[0].list.push(ps.process(item, env));
    }
    return result;
  },
  VariableDeclarator(node, env) {
    const result = {
      type: 'VariableDeclarator',
      links: [{
        name: 'id',
        list: [ps.process(node.id, env)],
      }],
    };
    if (node.init) result.links.push({
      name: 'init',
      list: [ps.process(node.init, env)],
    });
    return result;
  },
  Identifier(node, env) {
    return {
      type: 'Identifier',
      value: node.name,
    };
  },
  Literal(node, env) {
    return {
      type: 'Literal',
      value: node.raw,
    };
  },
  WhileStatement(node, env) {
    return {
      type: 'WhileStatement',
      links: [{
        name: 'test',
        list: [ps.process(node.test, env)],
      }, {
        name: 'body',
        list: [ps.process(node.body, env)],
      }],
    };
  },
  IfStatement(node, env) {
    return {
      type: 'IfStatement',
      links: [{
        name: 'test',
        list: [ps.process(node.test, env)],
      }, {
        name: 'consequent',
        list: [ps.process(node.consequent, env)],
      }, {
        name: 'alternate',
        list: [ps.process(node.alternate, env)],
      }],
    };
  },
  BinaryExpression(node, env) {
    return {
      type: 'BinaryExpression',
      value: node.operator,
      links: [{
        name: 'left',
        list: [ps.process(node.left, env)],
      }, {
        name: 'right',
        list: [ps.process(node.right, env)],
      }],
    };
  },
  BlockStatement(node, env) {
    const result = {
      type: 'BlockStatement',
      links: [{
        name: 'body',
        list: [],
      }],
    };
    let item;
    for (item of node.body) {
      result.links[0].list.push(ps.process(item, env));
    }
    env.root = result;
    return result;
  },
  ExpressionStatement(node, env) {
    return {
      type: 'ExpressionStatement',
      links: [{
        name: 'expression',
        list: [ps.process(node.expression, env)],
      }],
    };
  },
  AssignmentExpression(node, env) {
    return {
      type: 'AssignmentExpression',
      value: node.operator,
      links: [{
        name: 'left',
        list: [ps.process(node.left, env)],
      }, {
        name: 'right',
        list: [ps.process(node.right, env)],
      }],
    };
  },
  process(node, env) {
    const p = ps[node.type];
    if (!p) {
      console.error('未处理的类型', node.type, node);
      return;
    }
    const result = p(node, env);
    return result;
  },
};

export default ps;
